#include <stdio.h>
#include <stdlib.h>
#include <sys/unistd.h>

// #define N 1180  // 16MB版本矩阵尺寸
#define N 1320  // 20MB版本矩阵尺寸

int main() {
    int size = N * N * sizeof(int);

    printf("\033[0m"WARN"Matrix multiplication test size:%d x %d\033[0m\n", N, N);
    
    // 使用mmap分配三个矩阵内存
    int *A = mmap(NULL, size, PROT_READ|PROT_WRITE, MMAP_ANONYMOUS, -1, 0);
    int *B = mmap(NULL, size, PROT_READ|PROT_WRITE, MMAP_ANONYMOUS, -1, 0);
    int *C = mmap(NULL, size, PROT_READ|PROT_WRITE, MMAP_ANONYMOUS, -1, 0);

    // 初始化矩阵（全1矩阵）
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            A[i*N + j] = 1;
            B[i*N + j] = 1;
        }
    }

    // 矩阵乘法计算
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            int sum = 0;
            for (int k = 0; k < N; k++) {
                sum += A[i*N + k] * B[k*N + j];
            }
            C[i*N + j] = sum;
        }
    }

    // 验证结果正确性
    int is_correct = 1;
    const int expected = N;
    for (int i = 0; i < N && is_correct; i++) {
        for (int j = 0; j < N && is_correct; j++) {
            if (C[i*N + j] != expected) {
                is_correct = 0;
            }
        }
    }

    printf("\033[0m"PASS"Matrix multiplication test %s!\033[0m\n", is_correct ? "passed" : "failed");
    return 0;
}